

class Upload{
constructor(){}

// 上传图片接口
 uploadImg(file,_this,blob) {
   console.log(file)
  return new Promise((resolve, reject)=>{
    let url = "https://tiansuanshuke.oss-accelerate.aliyuncs.com";
    var testmsg = /^image\/(jpeg|png|jpg|gif)$/.test(file.file.type);
    if (!testmsg) {
      _this.$message.error('上传图片格式不对!')
      return;
    }
    
    var obj = file.file.name;
    var index = obj.lastIndexOf(".");
    obj = obj.substring(index, obj.length);
    // 生成的文件名，保留文件后缀名，进行拼接
    let random = Math.round(new Date() / 1000) + Math.ceil(Math.random() * 10) + file.file.uid?file.file.uid:file.uid;
    // 获取年月日
    var date = new Date(); //中国标准时间
    var year = date.getFullYear(); //获取完整的年份(4位)
    var month = date.getMonth(); //获取当前月份(0-11,0代表1月)
    var nowDate = date.getDate(); //获取当前日(1-31)
    let fileName = `${year}/${month + 1}/${nowDate}/${random}${obj}`;
    
    // 组装formdata
    let oss = {
      policy:"eyJleHBpcmF0aW9uIjoiMjA1MC0wMS0wMVQxMjowMDowMC4wMDBaIiwiY29uZGl0aW9ucyI6W1siY29udGVudC1sZW5ndGgtcmFuZ2UiLDAsMTA0ODU3NjAwMF1dfQ==",
      host: "tiansuanshuke.oss-accelerate.aliyuncs.com",
      OSSAccessKeyId: "LTAI5tKemD8uwDqQWAnte5tc",
      success_action_status: "200",
      signature: "CD6vG3LgEjmcuDG/bA94OWYt+ME="
    };
    
    let formdata = new FormData();
    formdata.append("policy", oss.policy);
    formdata.append("host", oss.host);
    formdata.append("OSSAccessKeyId", oss.OSSAccessKeyId);
    formdata.append("success_action_status", oss.success_action_status);
    formdata.append("signature", oss.signature);
    formdata.append("key", fileName);
    formdata.append("file", blob!=undefined?blob:file.file);
    _this.$axios
      .post(url, formdata)
      .then(res => {
      if (res.status == 200) {
          resolve({type:true,url:url + "/" + fileName});
      } else {
        reject({type:false});
      }
      })
      .catch(err => {
      reject({type:false});
      });
    });
  }






}

export default Upload;